package Power_Asisst.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import process.SendFileUDP;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ConnectSuccessfull extends Activity{
	Button sendfile;
	Button choosefile;
	Button showfile;
	private String[] IP ;
	Untils svUntil = new Untils();
	int size = 0;
	String[]ipArray;
	String host = "";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.connectsuccessfull);
		sendfile = (Button)findViewById(R.id.btnSendFile);
		choosefile  = (Button)findViewById(R.id.btnChooseFile);
		showfile = (Button)findViewById(R.id.btnShowFile);
		 Bundle bundle = getIntent().getExtras();
		 String GiaoThuc = bundle.getString("Giao");
	 	size=bundle.getInt("sizeOfIP");
	 	ipArray=bundle.getStringArray("IP");
	 	IP=svUntil.IPSlipt(ipArray);
	 	final int port = 8282;
	 	final String thongtin = "GoiDanhSach";
		sendfile.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
		    	intent.setClass(getBaseContext(),SdCard.class);
		    	intent.putExtra("sizeOfIP",size);
		    	intent.putExtra("IP", ipArray);
		    	intent.putExtra("Giao", "UDP");
				startActivity(intent);
			}
		});
		
		choosefile.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SendFileUDP u = new SendFileUDP();
				
				for(int i=0;i<IP.length;i++)
				{
					 host = IP[i];
					int kq = u.sendThongTin(host, port, thongtin);
					if(kq ==1)
					{
						try {
							int ports =8484;
							ServerSocket serverSocket = new ServerSocket(ports);
							Socket client = serverSocket.accept();
							 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
		                        final String line=in.readLine();
		                        if(line!= "")
		                        {
		                        	Intent intent1 = new Intent();
		            		    	intent1.setClass(getBaseContext(),ListPowerpoint.class);
		            		    	intent1.putExtra("DanhSachFile",line);
		            		    	intent1.putExtra("IP", IP);
		            		    	intent1.putExtra("Port", port);
		            		    	intent1.putExtra("Giao", "UDP");
		            				startActivity(intent1);
		                        }
		                        Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT);
		                        String []b = line.split(";");
		                        
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		});
		
		showfile.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String noidung = "ShowFile";
				SendFileUDP u = new SendFileUDP();
				for(int i=0;i<IP.length;i++)
				{
					host = IP[i];
					int kq = u.sendThongTin(host, port, noidung);
					Intent intent1 = new Intent();
					intent1.setClass(getBaseContext(),RemotePowerPoint.class);
			    	intent1.putExtra("IP", IP);
			    	intent1.putExtra("Giao", "UDP");
			    	startActivity(intent1);
				}
				
				
			}
		});
	}
}
